import pandas as pd
from pandas import DataFrame

import sys
import os
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')

datafile = './文本/客运量_1-1.csv' 
datafile2 = './文本/货运量_1-1.csv'
datafile3 = './文本/客运量_1-15.csv'
datafile4 = './文本/货运量_1-15.csv'

data = pd.DataFrame(pd.read_csv(datafile))
data2 = pd.DataFrame(pd.read_csv(datafile2))

#客运量
#找到异常的机场
b = list(set((data[data['Year'] == 2018]['Airport name'])).difference(set((data[data['Year'] == 2007]['Airport name']))))
indexa = []
#获取行数
for i in range(len(b)):
    index1 = data.loc[data['Airport name'] == b[i]].index.tolist()
    indexa.append(index1)
#删除异常机场所在的行
for i in range(len(indexa)):
    data = data.drop(index=indexa[i])

#删除后发现2018年的机场缺少2个，下面找到异常的机场，同上处理
d = list(set((data[data['Year'] == 2010]['Airport name'])).difference(set((data[data['Year'] == 2018]['Airport name']))))
indexb = []
for i in range(len(d)):
    index1 = data.loc[data['Airport name'] == d[i]].index.tolist()
    indexb.append(index1)
for i in range(len(indexb)):
    data = data.drop(index=indexb[i])

#删除后发现有些机场不在2019出现，这类机场也删除
x = list(set((data[data['Year'] == 2010]['Airport name'])).difference(set((data[data['Year'] == 2019]['Airport name']))))
index = []
for i in range(len(x)):
    index1 = data.loc[data['Airport name'] == x[i]].index.tolist()
    index.append(index1)
for i in range(len(index)):
    data = data.drop(index=index[i])

#同时发现有些机场仅在2019出现，这类机场也删除
x2 = list(set((data[data['Year'] == 2019]['Airport name'])).difference(set((data[data['Year'] == 2010]['Airport name']))))
index2 = []
for i in range(len(x2)):
    index1 = data.loc[data['Airport name'] == x2[i]].index.tolist()
    index2.append(index1)
for i in range(len(index2)):
    data = data.drop(index=index2[i])
#保存
data.to_csv(datafile3,index=False,encoding='utf_8_sig')


# #货运量，处理思路同上
# #找到异常的机场
b = list(set((data2[data2['Year'] == 2018]['Airport name'])).difference(set((data2[data2['Year'] == 2007]['Airport name']))))
indexc = []
#获取行数
for i in range(len(b)):
    index1 = data2.loc[data2['Airport name'] == b[i]].index.tolist()
    indexc.append(index1)
#删除异常机场所在的行
for i in range(len(indexc)):
    data2 = data2.drop(index=indexc[i])

a = list(set((data2[data2['Year'] == 2010]['Airport name'])).difference(set((data2[data2['Year'] == 2018]['Airport name']))))
indexd = []
for i in range(len(d)):
    index1 = data2.loc[data2['Airport name'] == a[i]].index.tolist()
    indexd.append(index1)
for i in range(len(indexd)):
    data2 = data2.drop(index=indexd[i])

y = list(set((data2[data2['Year'] == 2010]['Airport name'])).difference(set((data2[data2['Year'] == 2019]['Airport name']))))
indexy = []
for i in range(len(y)):
    index1 = data2.loc[data2['Airport name'] == y[i]].index.tolist()
    indexy.append(index1)
for i in range(len(indexy)):
    data2 = data2.drop(index=indexy[i])

y2 = list(set((data2[data2['Year'] == 2019]['Airport name'])).difference(set((data2[data2['Year'] == 2010]['Airport name']))))
indexy2 = []
for i in range(len(y2)):
    index1 = data2.loc[data2['Airport name'] == y2[i]].index.tolist()
    indexy2.append(index1)
for i in range(len(indexy2)):
    data2 = data2.drop(index=indexy2[i])
#保存
data2.to_csv(datafile4,index=False,encoding='utf_8_sig')